<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="/">商品管理</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-alert title="注意：只允许为第三级分类设置相关参数！" type="warning" show-icon :closable="false"></el-alert>
      <el-row class="cat_opt">
        <el-col>
          <span>选择商品分类:</span>
          <el-cascader
            v-model="categoriesKey"
            :options="categoriesList"
            :show-all-levels="true"
            :props="cateProps"
            @change="seleGoodsH"
          ></el-cascader>
        </el-col>
      </el-row>
      <el-tabs v-model="activeName" @tab-click="handleClick" border>
        <el-tab-pane label="动态参数" name="many">
          <el-button type="primary" size="mini" :disabled="isBtndisable" @click="addFromBtn">添加参数</el-button>
          <el-table :data="manyTableData" style="width: 100%">
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-tag
                  v-for="(item,i) in scope.row.attr_vals"
                  :key="i"
                  closable
                  type="success"
                >{{item}}</el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                   @blur="handleInputConfirm(scope.row)"
                ></el-input>
                <el-button
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                >+ New Tag</el-button>
              </template>
            </el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column label="参数名称" prop="attr_name" align="center"></el-table-column>
            <el-table-column label="操作" prop="attr_name">
              <template slot-scope="scope">
                <el-button size="mini" round icon="el-icon-edit" type="primary">修改</el-button>
                <el-button size="mini" round icon="el-icon-delete" type="danger">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button type="primary" size="mini" :disabled="isBtndisable" @click="addFromBtn">添加属性</el-button>
          <el-table :data="onlyTableData" style="width: 100%">
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-tag
                  v-for="(item,i) in scope.row.attr_vals"
                  :key="i"
                  closable
                  type="success"
                >{{item}}</el-tag>
                <el-button class="button-new-tag" size="small">+ New Tag</el-button>
              </template>
            </el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column label="参数名称" prop="attr_name"></el-table-column>
            <el-table-column label="操作" prop="attr_name">
              <template slot-scope="scope">
                <el-button size="mini" round icon="el-icon-edit" type="primary">修改</el-button>
                <el-button size="mini" round icon="el-icon-delete" type="danger">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <el-dialog :title="titles" :visible.sync="dialogFormVisible" width="50%">
      <el-form :model="addFrom" label-width="100px">
        <el-form-item label="动态参数" prop="attr_name">
          <el-input v-model="addFrom.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      activeName: 'many',
      categoriesList: [],
      categoriesKey: [],
      // 动态
      manyTableData: [],
      addFrom: {
        attr_name: ''
      },
      dialogFormVisible: false,
      //   静态属性数据
      onlyTableData: [],
      titles: "tte",
      cateProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
    }
  },
  computed: {
    isBtndisable () {
      console.log('isBtndisable')
      console.log(this.categoriesKey.length)
      if (this.categoriesKey.length !== 3) {
        return true
      }
      return false;
    },
    cateId () {
      if (this.categoriesKey.length === 3) {
        return this.categoriesKey[2]
      }
      return null
    }
  },
  created () {
    this.getCategoriesList()
  },
  methods: {
    showInput (row) {
      row.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    saveOrUpdate () {

    },
    addFromBtn () {
      if (this.activeName === 'only') {
        this.titles = '添加静态参数'
      } else {
        this.titles = '添加动态参数'
      }

      this.dialogFormVisible = true
    },
    handleClick () {
      console.log(this.activeName)
      this.seleGoodsH()
    },
    async seleGoodsH () {
      console.log(this.categoriesKey)
      if (this.categoriesKey.length !== 3) {
        this.categoriesKey = []

      }

      const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes`, {
        params: { sel: this.activeName }
      })
      res.data.forEach(item => {
        //   通过三元表达式判断attr_vals是否为空
        item.attr_vals = item.attr_vals ? item.attr_vals.split(' ') : []

        // 控制文本框的显示与隐藏
        item.inputVisible = false
        // 文本框的输入值
        item.inputValue = ''
      })
      console.log(res)
      if (this.activeName == 'only') {
        this.onlyTableData = res.data;
      } else {
        this.manyTableData = res.data;
      }
    },
    async getCategoriesList () {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) {
        return this.$message.error("获取分类失败")
      }
      this.categoriesList = res.data
    }
  }
}
</script>
<style lang="less" scope>
.cat_opt {
  margin: 15px 0px;
}
.el-tag {
  margin: 8px;
}
.button-new-tag {
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
}
</style>